Week 5 Homework


1. Write a function cubed() that uses the power() function to compute the cube (power of 3) of a number.

2. Write a python script to cube teh values in the input.txt file and save it as cubed.txt

Your code must:

  • Have a function named "cube()"
  • The cubed function must:
    • Take only one parameter, the number to be cubed
    • Return the cubed number
    • Call the comp_power() function (it cannot cube the number on its' own)
  • Open the input file input.txt
  • Loop through each value in the input file
  • Calculate the cube of each number using your function cubed()
  • Save the cubed value to the cubed.txt output file

Please submit your python code as your_name_cubed.py.

Please drop it here:

https://dropitto.me/BIOF309-HW-Week5

I will campare your results to the file name cubed_answers.txt


In [74]:
# Recall that the power function we used in class is:

def power(x, y):
    value = int(x)**int(y)
    return value

In [13]:
#def cubed(num):
    # raise to the power of three
#    power(num, 3)

In [75]:
def cubed(num):
    result = power(num, 3)
    return result

In [ ]:
def cubed(num):
    number = int(num)
    result = power(number, 3)
    return result

In [60]:
int(1)


Out[60]:
1

In [14]:
#def cubed(x):
#    return x**3

In [20]:
print(cubed(3))


27

In [22]:
!ls -la


total 112
drwxr-xr-x   9 squiresrb  NIH\Domain Users    306 Oct 19 18:05 .
drwxr-xr-x  14 squiresrb  NIH\Domain Users    476 Oct 18 00:37 ..
-rw-r--r--@  1 squiresrb  NIH\Domain Users   6148 Oct 12 16:52 .DS_Store
drwxr-xr-x   6 squiresrb  NIH\Domain Users    204 Oct 19 17:28 .ipynb_checkpoints
-rw-r--r--   1 squiresrb  NIH\Domain Users  20709 Oct 12 23:25 Week05 - 02 - Functions.ipynb
-rw-rw-r--   1 squiresrb  NIH\Domain Users   4500 Oct 19 18:05 Week05 - 03 - Homework - Solved.ipynb
-rw-r--r--   1 squiresrb  NIH\Domain Users   2262 Oct 12 23:25 Week05 - 03 - Homework.ipynb
-rw-r--r--   1 squiresrb  NIH\Domain Users   6802 Oct 12 23:25 Why should you have functions?.ipynb
-rw-rw-r--   1 squiresrb  NIH\Domain Users     34 Oct 12 14:51 input.txt

In [83]:
file_connection = open("input.txt")
numbers = file_connection.read()
numbers


Out[83]:
'1\n3\n7 \n11\n13\n17\n23\n101\n107\n111\n123'

In [32]:
!pwd


/Users/squiresrb/Documents/version_control/BIOF309-2016-Fall/Week_05

In [38]:
import os
os.getcwd()


Out[38]:
'/Users/squiresrb/Documents/version_control/BIOF309-2016-Fall/Week_05'

In [45]:
type(numbers)


Out[45]:
str

In [46]:
help(numbers.split)


Help on built-in function split:

split(...) method of builtins.str instance
    S.split(sep=None, maxsplit=-1) -> list of strings
    
    Return a list of the words in S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
    removed from the result.


In [49]:
numbers_list = numbers.split()
numbers_list


Out[49]:
['1', '3', '7', '11', '13', '17', '23', '101', '107', '111', '123']

In [59]:
for item in numbers_list:
    print(cubed(item))


1
27
343
1331
2197
4913
12167
1030301
1225043
1367631
1860867

In [77]:
output_connection = open("cubed.txt", 'w')

In [78]:
for item in numbers_list:
    cubed_number = cubed(item)
    output_connection.write(str(cubed_number) + "\n")

In [73]:
output_connection.close()

In [79]:
from power_biof309 import power

In [82]:
# Recall that the power function we used in class is:

def power(x, y):
    value = int(x)**int(y)
    return value

def cubed(num):
    number = int(num)
    result = power(number, 3)
    return result

file_connection = open("input.txt")
numbers = file_connection.read()

numbers_list = numbers.split()

output_connection = open("cubed-test.txt", 'w')

for item in numbers_list:
    cubed_number = cubed(item)
    output_connection.write(str(cubed_number) + "\n")
    
output_connection.close()

In [5]:
def power(x, y):
    return int(str.strip([x]))**int(y)

def cubed(num):
    return power(num, 3)

file_connection = open("input.txt")
output_connection = open("cubed-test.txt", 'w')

for line in file_connection.readline():
    line
    #cubed_number = cubed(line)
    #output_connection.write(str(cubed_number) + "\n")
    
file_connection.close()
output_connection.close()

In [6]:
file_connection.readline?

In [ ]:


In [ ]: